<?php
/**
 * Description of 
 *
 * @author Marco Mounzer
 * @email m.mounzer@gmail.com
 * @copyright 2013
 */
require_once '../lib/begin.php';

if (!isset($_SESSION['user']) || get_class($_SESSION['user']) != 'WebUser')
    header('location: /');

require_once 'WebCar.php';
require_once 'WebPhoto.php';

$obj = new WebCar();

try {
    if (isset($_POST['_save'])) {
        if (isset($_POST['solo_photo'])) {
            $obj = Database::GetIstance()->loadObject($obj, $_REQUEST['id']);
            $obj->photo = Database::GetIstance()->loadObject(new WebPhoto(), $_POST['photo_id']);
        } else
            $obj->setObjectFromFormSubmit($_POST);

        $obj = Database::SaveOrUpdateObject($obj);

        if (!isset($_POST['solo_photo']))
            header('location: /admin/car.php?id=' . $obj->Id);

        die;
    } else if (isset($_POST['_delete'])) {
        $obj->setObjectFromFormSubmit($_POST);

        $photos = Database::GetIstance()->loadObjects(new WebPhoto, 'car_id = ' . Database::GetIstance()->quote($obj->Id));

        foreach ($photos as $photo) {
            $photo->delete();
        }

        if (Database::DeleteObject($obj)) {
            require_once '_header.php';
            echo "<br /><br />Elemento eliminato";
            require_once '_footer.php';
            die;
        }
    } else if (isset($_REQUEST['id'])) {
        $obj = Database::GetIstance()->loadObject($obj, $_REQUEST['id']);
    }
} catch (PDOException $e) {
    $err = '<b/>Si è verificato un errore: ' . $e->getMessage() . '<br />Error code: ' . $e->getCode() . '<br />Dati non salvati</b>';
} catch (Exception $e) {
    $err = '<b/>Si è verificato un errore: ' . $e->getMessage() . '<br />Error code: ' . $e->getCode() . '<br />Dati non salvati</b>';
}

require_once '_header.php';

if (isset($err))
    echo $err;

if ($obj === false) {
    echo "Nessun dato trovato.";
    require_once '_footer.php';
    die;
}
$img = $obj->photo->Id . '.' . $obj->photo->extension;

if (file_exists($uploaddir . $img))
    $img = '/images/cars/' . $img;
else
    $img = '/images/automoda.png';
?>

<script type="text/javascript">

    $(document).ready(function() {
        $("#infobox").dialog({
            autoOpen: false,
            resizable: false,
            modal: true,
            width: 'auto',
            show: {
                effect: "highlight",
                duration: 500
            },
            buttons: {
                Upload: function() {
                    $('#photoupload').submit();
                },
                Cancel: function() {
                    $( this ).dialog( "close" );
                }
            }
        });  
    });
            
    function showPageInInfobox(href){
        $("#infobox").load(href, function(){
            $("#infobox").dialog('open');
        });
    
    }
</script>

<table style="text-align: center; width: 700px;">
    <tr>
        <td style="vertical-align: top">
            <?php
            echo $obj->getFormHtml($obj->getForm());
            ?>
        </td>
        <td style="vertical-align: top">
            <?php
            require_once 'gallery.php';
            ?>
        </td>
    </tr>
    <?php
    if (isset($_REQUEST['id'])) {
        ?>
        <tr>
            <td colspan="2">
                <span style="text-decoration: underline; cursor: pointer;" onclick="showPageInInfobox('/admin/uploadphoto.php?car_id=<?php echo $obj->Id; ?>')">Aggiungi foto</span>
                <div id="infobox" title="Upload foto">
                    <span style="text-decoration: underline; cursor: pointer;" onclick="showPageInInfobox('/admin/uploadphoto.php?car_id=<?php echo $obj->Id; ?>')">Aggiungi foto</span>
                </div>
            </td>
        </tr>
        <?php
    }
    ?>

</table>
<?php
require_once '_footer.php';